// 以下代码是配置layui扩展模块的目录，以及加载主题
layui.config({
    base: 'js/modules/'
}).extend({
    miniAdmin: "layuimini/miniAdmin", // layuimini后台扩展
    miniMenu: "layuimini/miniMenu", // layuimini菜单扩展
    notice: 'notice/notice',//消息通知扩展
    miniTab: "layuimini/miniTab", // layuimini tab扩展
    miniTheme: "layuimini/miniTheme", // layuimini 主题扩展
    formSelects: 'formSelects/formSelects-v4',
    treeTable: 'newtreetable/treeTable',
    treetable: 'treetable/treetable',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    autocomplete: 'layui_complete/autocomplete',
    autocompleteOriginal: 'layui_complete/autocomplete_o',
    IconFonts: 'iconFonts/iconFonts',
    step: 'step-lay/step',
    admin: 'admin',
    bodyTab: 'bodyTab',
    echarts: 'echarts/echarts', // echarts图表扩展
    echartsTheme: 'echarts/echartsTheme', // echarts图表主题扩展
    dropdown: 'dropdown/dropdown', // 下拉菜单
    selectInput: 'selectInput/selectInput',  //selectInput 可输入选择框组件
    sliderVerify: 'sliderVerify/sliderVerify'  //滑块
}).use(['layer'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;

    // 加载设置的主题
    var theme = layui.data('easyweb').theme;
    if (theme) {
        layui.link('/assets/css/theme/' + theme + '.css');
    }

    // 移除loading动画
    setTimeout(function () {
        $('.page-loading').remove();
    }, window == top ? 500 : 300);


});

// 移除主题
function removeTheme() {
    layui.jquery('link[id^=layuicss-assetscsstheme]').remove();
}

var syncPost = function (url, type, data, success) {
    $.ajax({
        url: url,
        type: type,
        data: data,
        async: false,
        success: success,
        error: function () {
            top.layer.msg(data.msg, {icon: 2});
        }
    });
};
//测试环境
//var fastdfsurl = "http://124.117.223.46:8099/";
var fastdfsurl = "http://172.28.134.199:8085/";

function currentYear() {
    var nowDate = new Date();
    var year = nowDate.getFullYear().toString().substr(2, 2);
    return year;
}


/**
 * 字符串是否为空
 * @param str
 * @returns
 */
function isEmpty(str) {
    if (str == null || str == undefined || str == '') {
        return true;
    } else {
        return false;
    }
}

function Layui_SetDataTableRowColor(DivId, RowIndex, Color) {
    try {
        var div = document.getElementById(DivId);
        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName('layui-table-main');   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    if (trs != null && trs.length > 0) {
                        trs[RowIndex].style.color = Color;//字体颜色 
                        //trs[RowIndex].style.backgroundColor= Color;//背景颜色
                    }
                }
            }

        }
    } catch (e) {
        console.log(e.message);
    }
}


function Layui_SetDataTableRowBackGroundColor(DivId, RowIndex, Color) {
    try {
        var div = document.getElementById(DivId);
        if (div != null) //找到对象了
        {
            var table_main = div.getElementsByClassName('layui-table-main');   //通过class获取table_main
            if (table_main != null && table_main.length > 0) {
                var table = table_main[0].getElementsByClassName('layui-table');   //通过class获取table
                if (table != null && table.length > 0) {
                    var trs = table[0].querySelectorAll("tr");
                    if (trs != null && trs.length > 0) {
                        //trs[RowIndex].style.color = Color;//字体颜色 
                        trs[RowIndex].style.backgroundColor = Color;//背景颜色
                    }
                }
            }

            //单选或者复选框固定的列的背景色处理
            var table_main_fixed = div.getElementsByClassName('layui-table-fixed');   //通过class获取layui-table-fixed
            if (table_main_fixed != null && table_main_fixed.length > 0) {
                var table_body = table_main_fixed[0].getElementsByClassName('layui-table-body');   //layui-table-body
                if (table_body != null && table_body.length > 0) {
                    var table_ = table_body[0].getElementsByClassName('layui-table');   //通过class获取table
                    if (table_ != null && table_.length > 0) {
                        var trs = table_[0].querySelectorAll("tr");
                        if (trs != null && trs.length > 0) {
                            trs[RowIndex].style.backgroundColor = Color;//背景颜色
                        }
                    }

                }

            }

        }
    } catch (e) {
        console.log(e.message);
    }
}


var formatDate = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;

    return y + '-' + m + '-' + d;
};
/**
 * 字符串转日期格式
 * @param dateStr
 * @param separator
 * @returns {Date}
 */
var stringToDate = function(dateStr,separator){
    if(!separator){
        separator="-";
    }
    var dateArr = dateStr.split(separator);
    var year = parseInt(dateArr[0]);
    var month;
    //处理月份为04这样的情况
    if(dateArr[1].indexOf("0") == 0){
        month = parseInt(dateArr[1].substring(1));
    }else{
        month = parseInt(dateArr[1]);
    }
    var day = parseInt(dateArr[2]);
    var date = new Date(year,month -1,day);
    return date;
}
/**
 * 日期转字符串格式
 * @param date
 * @returns {string}
 */
var dateToString=function(date){
    var year = date.getFullYear();
    var month =(date.getMonth() + 1).toString();
    var day = (date.getDate()).toString();
    if (month.length == 1) {
        month = "0" + month;
    }
    if (day.length == 1) {
        day = "0" + day;
    }
    var dateTime = year + "-" + month + "-" + day;
    return dateTime;
}

/*获取系统当前日期*/
var getCurrentDate = function () {
    let currentDate;
    $.ajax({
        url: 'common/getNowDate',
        type: 'post',
        async: false,

        success: function (data) {
            currentDate = data;
        },
        error: function () {
            top.layer.msg("系统日期异常，请联系管理员", {icon: 2});
        }
    });
    return currentDate;

}


var intervalDay = function (startTime, endTime) {
    var atime = Date.parse(startTime.replace(/-/ig, '/'));
    ;    //返回1970到现在指定日期的秒数
    var btime = Date.parse(endTime.replace(/-/ig, '/'));
    ;    //返回1970到现在指定日期的秒数
    if (atime == btime) {
        return 0;
    }
    if (atime > btime) {
        var c = atime - btime;
        var miao = c / 1000;
        var fen = miao / 60;
        var shi = fen / 60;
        var day = Math.round((shi / 24) * 100) / 100;
        return day;
    }
    if (btime > atime) {
        var c = btime - atime;
        var miao = c / 1000;
        var fen = miao / 60;
        var shi = fen / 60;
        var day = Math.round((shi / 24) * 100) / 100;
        return day;
    }


}

//比较日期大小
function compareDate(startDate,endDate){

    var oDate1 = new Date(startDate);
    var oDate2 = new Date(endDate);
    if(oDate1.getTime() > oDate2.getTime()){
     return -1;
    } else  if(oDate1.getTime() == oDate2.getTime()){
        return 0;
    }else {
      return 1;
    }

}



function mul(a, b) {
    return (a * b).toFixed(2);
}
//自定义加减法运算 加法-add , 减法-reduce
function Calculation(type, num1, num2) {

    var temp1, temp2, a;
    try {
        // 获取temp1小数点后的长度
        temp1 = num1.toString().split(".")[1].length;
    } catch (e) {
        temp1 = 0;
    }
    try {
        // 获取temp2小数点后的长度
        temp2 = num2.toString().split(".")[1].length;
    } catch (e) {
        temp2 = 0;
    }
    // Math.max(temp1, temp2) 为了获取temp1和temp2两个值中较大的一个
    // Math.pow(a,b) 表示 a 的 b 次方
    a = Math.pow(10, Math.max(temp1, temp2));

    // 计算的方式是先将所有的小数乘为整数，待加减运算执行完之后再除去对应的 a 的值，将其变为小数输出
    // 先判断执行的方式是否是加法，不是的话则执行减法运算
    return type == "add" ? (num1 * a + num2 * a) / a : (num1 * a - num2 * a) / a;
}


/**
 * 保留兩位小数，不四舍五入
 * @param num
 * @param decimal
 * @returns {string}
 */
function formatDecimal(num, decimal) {
    num = num.toString()
    let index = num.indexOf('.')
    if (index !== -1) {
        num = num.substring(0, decimal + index + 1)
    } else {
        num = num.substring(0)
    }
    return parseFloat(num).toFixed(decimal)
}


function checkboxSelect(){
    $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
        var index = $(this).attr('data-index');
        var tableBox = $(this).parents('.layui-table-box');
        //存在固定列
        if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
            tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
        } else {
            tableDiv = tableBox.find(".layui-table-body.layui-table-main");
        }
        var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
        if (checkCell.length > 0) {
            checkCell.click();
        }
    });
//对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件（Table的单击行事件不会拦截，依然有效）
    $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
        e.stopPropagation();
    });

}
